import Introduce from '../view/introduce.vue'
import BaoMing from 'pages/baoMing/baoMing.vue'
import uniPopupShare from '../popup/uni-popup-share.vue'
import HongBao from '../../components/RedPacket/RedPacket.vue'
import RedList from '../view/redList.vue'
import RedPack from '../../components/pupUp/redPack.vue'
import CountDown from '../../components/countDown/countDown.vue'
import Coupon from '../view/coupon.vue'
import PlayVideo from '../../components/playVideo.vue'
import uParse from '../../components/gaoyia-parse/parse.vue'
export default {
	components: {
		Introduce,
		HongBao,
		BaoMing,
		RedList,
		CountDown,
		RedPack,
		uniPopupShare,
		Coupon,
		PlayVideo,
		uParse

	},

	data() {
		return {
			title: "商品详情",
			courseId: "",
			over: false,
			bodtyH: (this.WH - this.CustomBar) + 'px',
			modalName: "DialogModal1",
			isBuy: false, //是否已经购买了
			footH: this.CustomBar,
			background: ['color1', 'color2', 'color3'],
			indicatorDots: true,
			loadStatus: 'load',
			isStatus: false,
			autoplay: true,
			interval: 2000,
			isLoading: false,
			duration: 500,
			good: [], //商品
			introduce: '',
			TabCur: 0,
			videoUrl: '',
			showVideo: false,
			isShowTab: false, //是否显示头部条
			share_people: 0, //已分享的人数
			tapType: [{
					name: "商品",
					type: 0,
					id: "goodId",
					topH: ""
				},
				{
					name: "详情",
					type: 1,
					id: "introduceId",
					topH: ""
				}

			],
			isCoupon: false,
			msgList: [], //消息列表
			msgIndex: 0, //滚动的消息的下标
			isMsgopen: false, //消息滚动是否开启
			scrollP: 20, //滚动条的位置
			scrollp1: 0,
			socketTask: false, //socket任务的存储
			conponId: '',
			option: {},
			redPackData: {
				title: "",
				condition: "",
				userList: [],
				myInvitedOrderNum: ""

			}
		}
	},
	/**初始加载接收参数
	 * @param {Object} option
	 */
	onLoad(option) {
		this.addmsg();
		this.option = option;
		this.loadData();


	},

	onUnload(e){
		console.log(e);
		
	},
	/**
	 * 分享的操作
	 */
	onShareAppMessage(res) {
		console.log("分享的openid " + this.$openid);
		return {
			title: this.good.title,
			imageUrl: this.good.advertmap[0].url,
			path: '/pages/index/index?courseId=' + this.courseId + '&userCode=' + this.initCode
		}

	},
	onBackPress(options) {
		console.log("点击返回界面");
		console.log(options);
		if (options.from === 'navigateBack') {
			return false;
		}
		this.onreturn();
		return true;
	},
	methods: {

		loadData() {
			let option = this.option;
			let data = {
				action: "getCourse",
				courseId: option.courseId,
				openId: this.Reques.sessionData.data.openId
			};
			this.courseId = option.courseId;
			this.Reques.commonQuest("xcx/course.api.php", "POST", data).then((res) => {
				console.log(res);
				if (res.statusCode == 200 && res.data.code == 0) {
					this.isLoading = true;
					this.good = res.data.course[0];
					this.title = this.good.title;
					this.isBuy = res.data.course[0].signed;
					this.$nextTick(() => {
						this.introduce = res.data.course[0].introduce;
						this.redPackData.myInvitedOrderNum = this.good.myInvitedOrderNum;

					});
					this.changeSign();

				} else {
					this.loadStatus = 'error2';
				}
			}, (err) => {
				this.loadStatus = 'error2';
				console.log(err);
			});
			this.requestBaoming();

		},
		/**
		 * 回调更新
		 * @param {Object} type
		 */

		upData: function(type) {
			console.log("更新");
			this.isStatus = false;
			this.addmsg();
			this.loadData();

		},

		/**
		 * 判断是否授权
		 * @param {Object} d
		 */

		reciveAuth(d) {
			((d == true) ? this.baoming() : "");
			console.log("auth: " + d);
		},


		/**
		 * 点击播放人数
		 * @param {Object} s
		 * @param {Object} d]
		 */
		clicked(s, d) {
			console.log("播放人数");
		},

		playVideo() {
			console.log("播放视频");
		},
		/**
		 * 异步请求分享报名成工的人数
		 * @param {Object} coupon
		 */
		requestBaoming() {


			let requestMeth = () => {
				let dat = {
					action: "getHongbaoList",
					openId: this.Reques.sessionData.data.openId
				}
				this.Reques.commonQuest("xcx/course.api.php", "POST", dat).then((res) => {
					if (res.statusCode == 200 && res.data.code == 0) {
						this.good.hongbaoList = res.data.hongbaoList;
						this.redPackData.userList = res.data.myInvitedOrder;
						this.$refs.changestatus.xx(this.good.hongbaoList);
					}
				}, err => {
					console.log(err);

				});

			}
			setTimeout(() => {
				requestMeth();
			}, 1);
			let timer = setInterval(() => {
				requestMeth();
			}, 10000);

		},

		/**
		 * 优惠券界面的控制
		 */
		isCouponMeth(coupon) {

			this.isCoupon = (this.isCoupon == true) ? false : true;
			this.$refs.changeBaoming.changeCoupon(coupon);

		},
		/**
		 * 打开优惠券
		 */
		openConpon(id) {
			this.conponId = id;
			this.isCoupon = (this.isCoupon == true) ? false : true;
		},
		/**
		 * 更新课程时间
		 */
		updateTime(...d) {
			console.log("第一个参数" + d[0]);
			this.over = d[0];
		},
		/**
		 * 菜单栏
		 * @param {Object} e
		 */
		tabSelect(e) {

			this.TabCur = e.currentTarget.dataset.id;
			this.currentType = e.currentTarget.dataset.type;
			this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60;
			console.log("top " + this.tapType[this.currentType].id);
			let info = uni.createSelectorQuery().select("#" + this.tapType[this.currentType].id);
			info.boundingClientRect(data => {
				this.scrollP = data.top + this.scrollp1 - 135;
				console.log("data " + data.top);
			}).exec(function() {});

		},
		/**
		 * 改变报名状态
		 */
		changeSign() {
			if (this.isBuy == 1) {
				this.buttonT = "查看课程";
			}
		},
		/**
		 * 滑动顶部菜单
		 * @param {Object} e
		 */
		change(e) {
			this.current = e.detail.current
		},
		/**
		 * 点击报名
		 */
		baoming() {
			let method = {
				"00": () => { //判断是否登录
					uni.getStorage({
						key: 'userInfo',
						success: (res) => {
							this.isStatus = true;
						},
						fail: (err) => {
							uni.navigateTo({
								url: '/pages/login/login',
								animationType: 'slide-in-top'
							});
						}
					});
				},
				"01": function() {
					console.log("xxx" + this.id);
					uni.navigateTo({
						url: '/pages/mycourse/order?type=' + this.id + "&courseId=" + 1,
						animationType: 'slide-in-left'
					})
				}
			}

			let s = String(this.good.signed + "" + this.good.status);
			console.log("ssssssssss " + s);
			method[s].apply({
				id: this.good.status
			});


		},
		/**
		 * 滚动的监听
		 * @param {Object} e
		 */
		gundong(e) {

			this.scrollp1 = e.detail.scrollTop;
			this.isShowTab = (e.detail.scrollTop > this.StatusBar) ? true : false;

		},
		/**
		 * 关闭报名界面
		 */
		closeBaoming() { //点击关闭按钮
			if (this.isStatus) {
				this.loadData();
				this.isStatus = false;
			} else {
				this.isStatus = true;
			}

		},
		/**改变红包的状态
		 * @param {Object} status
		 */
		changeStatu(status) {
			console.log("改变了状态" + status);
			this.$refs.changestatus.changeRedPack();

		},
		/**打开红包
		 * @param {Object} dat
		 */
		openRed(dat) {

			this.$refs.RedPack.openRed(dat, this.redPackData.userList);

		},
		/**
		 * 消息列表
		 */
		addmsg() {
			this.Reques.commonQuest("xcx/message.api.php", "POST", {
				action: "getLastMessage"
			}).then((res) => {
				if (res.statusCode == 200 && res.data.code == 0) {

					this.msgList = (res.data.data).splice("");
				} else {
					this.msgList = ["获取信息失败!"];
				}

			}, (err) => {
				this.msgList = ["获取信息失败！请检查网络"];
			});
			let timer = setInterval(() => {
				this.msgIndex++;
				this.msgIndex = (this.msgIndex == this.msgList.length) ? 0 : this.msgIndex;
				this.isMsgopen = true;
				setTimeout(() => {
					this.isMsgopen = false;
				}, 500);
			}, 4000);
		}



	}



}
